import * as express from 'express'
import * as Main from '../services/main'
import companyInfoSv from '../services/companyInfoSV';
import getMemberInfo from '../services/getMemberInfoSV';
import apiSv from '../services/apiSV';

let routerAdmin = express.Router()
let routerPersonal = express.Router()
/** 基本数据 */
let baseData = Main.getData()


/** 获取设置数据 */
async function getMainData(req: express.Request, getCorpInfo = true) {
  try {
    let mainData: RecordData = baseData
    mainData['serverTime'] = Date.now()
    mainData['companyId'] = req.companyId
    if (getCorpInfo) {
      mainData['corpInfo'] = await companyInfoSv(req)
      mainData['userInfo'] = await getMemberInfo(req,'')
    }
    return mainData
  } catch (e) {
    return Promise.reject(e)
  }

}

routerAdmin.use("/:type(survey|offlineTraining)", async (req, res, next) => {
  let menuId = ""
  switch (req.params.type) {
    case "survey":
      menuId = "MENU-CORPSURVEY"
      break
    case "offlineTraining":
      menuId = "MENU-CORPTRAINING"
      break
  }
  req['ysMenuId'] = menuId
  next()
})

//进入页面路由  get匹配任意不包含“.”的字符
routerAdmin.get(/^((?!\.).)+$/, async (req, res, next) => {
  if (req.xhr || !req['ysMenuId']) return next()
  try {
    return res.render('appAdmin')
  } catch (e) {
    return next(e)
  }
})

routerPersonal.get(/^((?!\.).)+$/, async (req, res, next) => {
  if (req.xhr) return next()
  try {
    // let mainData = await getMainData(req)
    return res.render('app')
  } catch (e) {
    next(e)
  }
})

export { routerAdmin, routerPersonal }
